Home:ALL Converter>xtensor equivalent of numpy a[a>3] = 1

xtensor equivalent of numpy a[a>3] = 1

Ask Time:2021-08-02T05:42:56         Author:Colin

Json Formatter

Title says it - what is the xtensor equivalent of numpy's

# set all elements > 3 to 1
sometensor[sometensor > 3] = 1 

?

It looks like xt::filter works:

xt::filter(sometensor, sometensor > 3) = 1

But it also looks like the numpy version is much faster. I've build xtensor with xsimd, but it doesn't seem to help in this case. Is there a better, more simd-ish way to do it?

EDIT

I found filtration, which indeed is faster (by about 3x), but still slower than numpy (by about 10x)...

SOLUTION (thx Tom!)

a = xt::where(a > 0.5, 1.0, a);

Is the fastest of all - about 10x faster than filtration, so it looks like it's simd-d!

Author:Colin,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/68614578/xtensor-equivalent-of-numpy-aa3-1
yy